import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:rc_app/pages/work/index.dart';
class _MenuItem {
  String name;
  Icon icon;
  _MenuItem(this.name, this.icon);
}

class ContainerPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ContainerPageState();
  }
}

class _ContainerPageState extends State<ContainerPage> {
  int _curInx = 0;
  List<BottomNavigationBarItem> itemList;
  final List<_MenuItem> menus = [
    _MenuItem("工作", Icon(Icons.dashboard)),
    _MenuItem("聊天", Icon(Icons.chat)),
    _MenuItem("通讯录", Icon(Icons.contact_phone)),
    _MenuItem("我", Icon(Icons.account_box))
  ];
  List<Widget> pages;

  @override
  void initState() {
    super.initState();
    print('build container page...');
    if (itemList == null) {
      itemList = menus
          .map((item) => BottomNavigationBarItem(
                icon: item.icon,
                activeIcon: item.icon,
                title: Text(
                  item.name,
                ),
              ))
          .toList();
    }
    if(pages==null){
      pages = [
        WorkPage(),
        Center(child: Text('聊天')),
        Center(child: Text('通讯录')),
        Center(child: Text('我'))
      ];
    }
  }

  Widget _getPage(int index){
    return Offstage(
      offstage: _curInx != index,
      child: TickerMode(
        enabled: _curInx == index,
        child: pages[index],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.grey[50],
        child:Stack(
          children: <Widget>[
            _getPage(0),
            _getPage(1),
            _getPage(2),
            _getPage(3),
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _curInx,
        iconSize: 24,
        items: itemList,
        selectedFontSize: 12,
        unselectedFontSize: 12,
        unselectedItemColor: Colors.black45,
        selectedItemColor: Colors.blueAccent,
        type: BottomNavigationBarType.fixed,
        onTap:(int value){
          if(value!=_curInx){
            setState(() {
              _curInx = value;
            });
          }
        }
      ),
    );
  }
}
